﻿using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using YJKRevitStairs.StairsModel.CommonUnit;
using YJKRevitStairs.StairsModel.DataInfos;

namespace YJKRevitStairs.StairsModel.Kit
{
	public class LinearPlatformStairsKit : ProjectionDrawingKit
	{
		public LinearPlatformStairsKit(Autodesk.Revit.ApplicationServices.Application app) : base(app)
		{
		}

		private void CalcRunPosition(ref LinearPlatformStairs stairs)
		{
			LineRunData downRunData = stairs.m_DownRunData;
			LineRunData upRunData = stairs.m_UpRunData;
			BaseLandingData landData = stairs.m_LandData;
			double width = landData.Width;
			double dStairsWidth = stairs.m_dStairsWidth;
			double dStairsLength = stairs.m_dStairsLength;
			XYZ xyz = new XYZ(dStairsWidth, 0.0, 0.0);
			XYZ xyz2 = new XYZ(0.0, downRunData.RunLength, 0.0);
			XYZ xyz3 = new XYZ(0.0, upRunData.RunLength, 0.0);
			XYZ xyz4 = new XYZ(0.0, landData.Width, 0.0);
			XYZ zero = XYZ.Zero;
			XYZ xyz5 = zero + xyz;
			XYZ xyz6 = zero + xyz2;
			XYZ ptRunTopRight = xyz5 + xyz2;
			XYZ xyz7 = xyz6 + xyz4;
			XYZ xyz8 = xyz7 + xyz;
			XYZ ptRunTopLeft = xyz7 + xyz3;
			XYZ ptRunTopRight2 = xyz8 + xyz3;
			stairs.m_DownRunData.PtRunBottomLeft = zero;
			stairs.m_DownRunData.PtRunBottomRight = xyz5;
			stairs.m_DownRunData.PtRunTopLeft = xyz6;
			stairs.m_DownRunData.PtRunTopRight = ptRunTopRight;
			stairs.m_UpRunData.PtRunBottomLeft = xyz7;
			stairs.m_UpRunData.PtRunBottomRight = xyz8;
			stairs.m_UpRunData.PtRunTopLeft = ptRunTopLeft;
			stairs.m_UpRunData.PtRunTopRight = ptRunTopRight2;
		}

		private void addInsertPoint(ref List<XYZ> listInsertPoints, LinearPlatformStairs stairs)
		{
			LineRunData downRunData = stairs.m_DownRunData;
			LineRunData upRunData = stairs.m_UpRunData;
			listInsertPoints.Add(downRunData.PtRunBottomLeft);
			listInsertPoints.Add(upRunData.PtRunTopLeft);
			listInsertPoints.Add(upRunData.PtRunTopRight);
			listInsertPoints.Add(downRunData.PtRunBottomRight);
		}

		private void SetStairsOutSupportPoints(ref LinearPlatformStairs stairs)
		{
			LineRunData downRunData = stairs.m_DownRunData;
			LineRunData upRunData = stairs.m_UpRunData;
			stairs.m_listPointsSupport.Add(downRunData.PtRunBottomLeft);
			stairs.m_listPointsSupport.Add(downRunData.PtRunTopLeft);
			stairs.m_listPointsSupport.Add(upRunData.PtRunBottomLeft);
			stairs.m_listPointsSupport.Add(upRunData.PtRunTopLeft);
		}

		private void DrawLinearPlatformStairsRunCurves(ref LinearPlatformStairs param_stairs, bool bBottomUpStairs, bool bDrawNoteInfo)
		{
			LineRunData downRunData = param_stairs.m_DownRunData;
			LineRunData upRunData = param_stairs.m_UpRunData;
			if (bBottomUpStairs)
			{
				SingleRunCurveInfo singleRunCurveInfo = new SingleRunCurveInfo();
				singleRunCurveInfo.BottomElevation = downRunData.BottomElevation;
				base.DrawLinearRun(downRunData, downRunData.PtRunBottomLeft, downRunData.PtRunBottomRight, downRunData.PtRunTopRight, downRunData.PtRunTopLeft, ref singleRunCurveInfo, ref param_stairs.m_listOtherCurve, bDrawNoteInfo);
				param_stairs.m_listRunCurve.Add(singleRunCurveInfo);
				singleRunCurveInfo = new SingleRunCurveInfo();
				singleRunCurveInfo.BottomElevation = upRunData.BottomElevation;
				base.DrawLinearRun(upRunData, upRunData.PtRunBottomLeft, upRunData.PtRunBottomRight, upRunData.PtRunTopRight, upRunData.PtRunTopLeft, ref singleRunCurveInfo, ref param_stairs.m_listOtherCurve, bDrawNoteInfo);
				param_stairs.m_listRunCurve.Add(singleRunCurveInfo);
				return;
			}
			SingleRunCurveInfo singleRunCurveInfo2 = new SingleRunCurveInfo();
			singleRunCurveInfo2.BottomElevation = upRunData.BottomElevation;
			base.DrawLinearRun(upRunData, upRunData.PtRunTopRight, upRunData.PtRunTopLeft, upRunData.PtRunBottomLeft, upRunData.PtRunBottomRight, ref singleRunCurveInfo2, ref param_stairs.m_listOtherCurve, bDrawNoteInfo);
			param_stairs.m_listRunCurve.Add(singleRunCurveInfo2);
			singleRunCurveInfo2 = new SingleRunCurveInfo();
			singleRunCurveInfo2.BottomElevation = downRunData.BottomElevation;
			base.DrawLinearRun(downRunData, downRunData.PtRunTopRight, downRunData.PtRunTopLeft, downRunData.PtRunBottomLeft, downRunData.PtRunBottomRight, ref singleRunCurveInfo2, ref param_stairs.m_listOtherCurve, bDrawNoteInfo);
			param_stairs.m_listRunCurve.Add(singleRunCurveInfo2);
		}

		private void DrawLinearPlatformStairsLandingCurves(ref LinearPlatformStairs param_stairs)
		{
			LineRunData downRunData = param_stairs.m_DownRunData;
			LineRunData upRunData = param_stairs.m_UpRunData;
			List<CurveInfo> landCurves = base.CreateLandEdgeloop(new List<XYZ>
			{
				downRunData.PtRunTopLeft,
				upRunData.PtRunBottomLeft,
				upRunData.PtRunBottomRight,
				downRunData.PtRunTopRight
			});
			double bottomElevation = 0.0;
			LandingCurveInfo item = new LandingCurveInfo(landCurves, bottomElevation);
			param_stairs.m_listLandCurve.Add(item);
		}

		private void DrawLinearPlatformStairsBeamCurves(ref LinearPlatformStairs param_stairs, bool bBottomUpStairs, bool bBottom, bool bMid, bool bTop)
		{
			LineRunData downRunData = param_stairs.m_DownRunData;
			LineRunData upRunData = param_stairs.m_UpRunData;
			double num = param_stairs.m_BeamData.BeamWidth / 2.0;
			XYZ xyz = new XYZ(0.0, num, 0.0);
			XYZ ptS = XYZ.Zero;
			XYZ ptE = XYZ.Zero;
			if (bBottom)
			{
				List<CurveInfo> list = new List<CurveInfo>();
				if (bBottomUpStairs)
				{
					ptS = downRunData.PtRunBottomLeft - xyz;
					ptE = downRunData.PtRunBottomRight - xyz;
					list.Add(base.NewLine(PenColor.PC_Gray, DashStyle.Solid, 1f, ptS, ptE, xyz));
				}
				else
				{
					ptS = upRunData.PtRunTopLeft + xyz;
					ptE = upRunData.PtRunTopRight + xyz;
					list.Add(base.NewLine(PenColor.PC_Gray, DashStyle.Solid, 1f, ptS, ptE, -xyz));
				}
				double bottomElevation = 0.0;
				BeamCurveInfo beamCurveInfo = new BeamCurveInfo(list, bottomElevation);
				beamCurveInfo.m_Type = BeamType.BT_Bottom;
				beamCurveInfo.m_nFloorNum = 0;
				param_stairs.m_listBeamCurve.Add(beamCurveInfo);
			}
			if (bMid)
			{
				List<CurveInfo> list = new List<CurveInfo>();
				if (bBottomUpStairs)
				{
					ptS = downRunData.PtRunTopLeft + xyz;
					ptE = downRunData.PtRunTopRight + xyz;
					list.Add(base.NewLine(PenColor.PC_Gray, DashStyle.Solid, 1f, ptS, ptE, xyz.Normalize()));
					ptS = upRunData.PtRunBottomLeft - xyz;
					ptE = upRunData.PtRunBottomRight - xyz;
					list.Add(base.NewLine(PenColor.PC_Gray, DashStyle.Solid, 1f, ptS, ptE, null));
				}
				else
				{
					ptS = upRunData.PtRunBottomLeft - xyz;
					ptE = upRunData.PtRunBottomRight - xyz;
					list.Add(base.NewLine(PenColor.PC_Gray, DashStyle.Solid, 1f, ptS, ptE, -xyz.Normalize()));
					ptS = downRunData.PtRunTopLeft + xyz;
					ptE = downRunData.PtRunTopRight + xyz;
					list.Add(base.NewLine(PenColor.PC_Gray, DashStyle.Solid, 1f, ptS, ptE, null));
				}
				double bottomElevation2 = 0.0;
				BeamCurveInfo beamCurveInfo2 = new BeamCurveInfo(list, bottomElevation2);
				beamCurveInfo2.m_Type = BeamType.BT_Mid;
				beamCurveInfo2.m_nFloorNum = 0;
				param_stairs.m_listBeamCurve.Add(beamCurveInfo2);
			}
			if (bTop)
			{
				List<CurveInfo> list = new List<CurveInfo>();
				if (bBottomUpStairs)
				{
					ptS = upRunData.PtRunTopLeft + xyz;
					ptE = upRunData.PtRunTopRight + xyz;
					list.Add(base.NewLine(PenColor.PC_Gray, DashStyle.Solid, 1f, ptS, ptE, xyz));
				}
				else
				{
					ptS = downRunData.PtRunBottomLeft - xyz;
					ptE = downRunData.PtRunBottomRight - xyz;
					list.Add(base.NewLine(PenColor.PC_Gray, DashStyle.Solid, 1f, ptS, ptE, -xyz));
				}
				double bottomElevation3 = 0.0;
				BeamCurveInfo beamCurveInfo3 = new BeamCurveInfo(list, bottomElevation3);
				beamCurveInfo3.m_Type = BeamType.BT_Top;
				beamCurveInfo3.m_nFloorNum = 0;
				param_stairs.m_listBeamCurve.Add(beamCurveInfo3);
			}
		}

		private void DrawUpStairsNoteInfo(ref LinearPlatformStairs param_stairs, bool bBottomUpStairs)
		{
			LineRunData downRunData = param_stairs.m_DownRunData;
			LineRunData upRunData = param_stairs.m_UpRunData;
			XYZ xyz = XYZ.BasisY * 1.3;
			List<UpDownText> list = new List<UpDownText>();
			string sText = "上";
			if (bBottomUpStairs)
			{
				UpDownText item = new UpDownText(Geometry.CalculatMidPoint(downRunData.PtRunBottomLeft, downRunData.PtRunBottomRight) - xyz, sText);
				list.Add(item);
				base.DrawUpDownNoteInfo(ref param_stairs.m_listText, list);
				return;
			}
			UpDownText item2 = new UpDownText(Geometry.CalculatMidPoint(upRunData.PtRunTopLeft, upRunData.PtRunTopRight) + xyz, sText);
			list.Add(item2);
			base.DrawUpDownNoteInfo(ref param_stairs.m_listText, list);
		}

		private void DrawLinearPlatformStairsNoteInfo(ref LinearPlatformStairs param_stairs)
		{
			LineRunData downRunData = param_stairs.m_DownRunData;
			LineRunData upRunData = param_stairs.m_UpRunData;
			XYZ xyz = XYZ.BasisY * 2.0;
			XYZ xyz2 = XYZ.BasisX * 2.0;
			XYZ ptStart = XYZ.Zero;
			XYZ ptEnd = XYZ.Zero;
			ptStart = upRunData.PtRunTopLeft + xyz;
			ptEnd = upRunData.PtRunTopRight + xyz;
			base.DrawStairsNoteInfo(ref param_stairs.m_listOtherCurve, ref param_stairs.m_listText, ptStart, ptEnd, "梯间宽度", 0.8, 5.0, 2.0, XYZ.BasisY, PenColor.PC_Black);
			ptStart = upRunData.PtRunTopLeft - xyz2;
			ptEnd = downRunData.PtRunBottomLeft - xyz2;
			base.DrawStairsNoteInfo(ref param_stairs.m_listOtherCurve, ref param_stairs.m_listText, ptStart, ptEnd, "梯间长度", 0.8, 2.0, 5.0, -XYZ.BasisX, PenColor.PC_Black);
			ptStart = upRunData.PtRunBottomRight + xyz2;
			ptEnd = downRunData.PtRunTopRight + xyz2;
			base.DrawStairsNoteInfo(ref param_stairs.m_listOtherCurve, ref param_stairs.m_listText, ptStart, ptEnd, "平台宽度", 0.8, 2.0, 5.0, XYZ.BasisX, PenColor.PC_Black);
		}

		public bool LinearPlatformStairsProfileCurves(ref LinearPlatformStairs stairs, bool bDrawNoteInfo, int basePointIndex, bool lrMirror, bool tbMirror, bool bBottomUpStairs, double rotateAngle, bool bBottom, bool bMid, bool bTop)
		{
			bool result = true;
			double dStairsWidth = stairs.m_dStairsWidth;
			double dStairsLength = stairs.m_dStairsLength;
			this.CalcRunPosition(ref stairs);
			List<XYZ> list = new List<XYZ>();
			this.addInsertPoint(ref list, stairs);
			this.SetStairsOutSupportPoints(ref stairs);
			XYZ xyz = list[basePointIndex];
			base.CalulateTransform(lrMirror, tbMirror, rotateAngle, dStairsWidth, dStairsLength, xyz);
			stairs.m_TransVector = Geometry.TransformPoint(xyz, this.m_Transform);
			stairs.m_listOtherCurve.AddRange(base.DrawBasePoint(xyz, 100.0));
			stairs.m_Transform = this.m_Transform;
			this.DrawLinearPlatformStairsRunCurves(ref stairs, bBottomUpStairs, bDrawNoteInfo);
			this.DrawLinearPlatformStairsLandingCurves(ref stairs);
			this.DrawLinearPlatformStairsBeamCurves(ref stairs, bBottomUpStairs, bBottom, bMid, bTop);
			if (bDrawNoteInfo)
			{
				this.DrawUpStairsNoteInfo(ref stairs, bBottomUpStairs);
			}
			this.DrawLinearPlatformStairsNoteInfo(ref stairs);
			return result;
		}
	}
}
